//
//  UIImageView+DF.m
//  换换圈
//
//  Created by 艾柯斯 on 14-11-7.
//  Copyright (c) 2014年 艾柯斯. All rights reserved.
//

#import "UIImageView+DF.h"
#import <objc/runtime.h>
#import "UIView+DF.h"


#define kScreenBackgroundView @"DFScreenBackgroundView"

@implementation UIImageView (DF)

+ (instancetype)imageViewWithName:(NSString*)imageName {
    return [[self alloc] initWithImageName:imageName];
}

-(instancetype)initWithImageName:(NSString *)imageName {
    return [self initWithImage:[UIImage imageNamed:imageName]];
}

-(CGSize)autoSize {
    if(!self.image) {
        self.size = CGSizeZero;
    }
    self.size =  self.image.size;
    return self.size;
}

- (void)showImageToScreen:(void (^)(UIView *))completion {
    UIView* screenBackgroundView = objc_getAssociatedObject(self,kScreenBackgroundView);
    if(screenBackgroundView) {
        return;
    }

    screenBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    screenBackgroundView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    objc_setAssociatedObject(self,kScreenBackgroundView,screenBackgroundView,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    UIImage* image = self.image;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = [self convertRect:(CGRect){CGPointZero,self.bounds.size} toView:window];
    imageView.image = image;
    imageView.contentMode = self.contentMode;
    
    [screenBackgroundView addSubview:imageView];
    [window addSubview:screenBackgroundView];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
        screenBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        
        imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
        
    } completion:^(BOOL finished) {
        if(completion) {
            completion(screenBackgroundView);
        }
    }];
}


- (void)showImageToScreen {
    [self showImageToScreen:^(UIView *screenBackgroundView) {
        [screenBackgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageForScreen)]];
    }];
    
}

- (void)hideImageForScreen {
    UIView* screenBackgroundView = objc_getAssociatedObject(self,kScreenBackgroundView);
    if(!screenBackgroundView) {
        return;
    }
    objc_setAssociatedObject(self,kScreenBackgroundView,nil,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    UIImageView *imageView = [screenBackgroundView.subviews lastObject];
    
    [UIView animateWithDuration:0.3 animations:^{
        screenBackgroundView.alpha = 0;
        self.alpha = 1;
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        CGRect frame = [self convertRect:(CGRect){CGPointZero,self.bounds.size} toView:window];//关键代码，坐标系转换
        imageView.frame = frame;
    } completion:^(BOOL finished) {
        [screenBackgroundView removeFromSuperview];
    }];
    
    
}
- (void)ShowImageToScreen {
    [self showImageToScreen:^(UIView *screenBackgroundView) {
        [screenBackgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageForScreen)]];
        [screenBackgroundView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(download:)]];
    }];
    
}

@end
